﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CApAPPPlanCommandBase
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.Collections.Generic;


namespace DnProcessSimulateCommands;

public class CApAPPPlanCommandBase
{
  private List<CApAPPTask> m_tasksToPlan;
  private KiAppPathPlanningParamsEx m_pathPlanningParams;
  private double m_progressRatio = 1.0;

  public CApAPPPlanCommandBase(
    List<CApAPPTask> i_tasks,
    KiAppPathPlanningParamsEx i_pathPlanningParams)
  {
    this.m_tasksToPlan = i_tasks;
    this.m_pathPlanningParams = i_pathPlanningParams;
  }

  public event PlanningDidEndEventHandler PlanningDidEnd;

  public event PlanningDidProgressEventHandler PlanningDidProgress;

  public event PlanningDidUpdateEventHandler PlanningDidUpdate;

  public event WaypointTypeDidChangedEventHandler WaypointDidChanged;

  public virtual void Cancel()
  {
  }

  public virtual void CancelPlanning(int i_taskRank)
  {
  }

  protected List<CApAPPTask> TasksToPlan => this.m_tasksToPlan;

  protected KiAppPathPlanningParamsEx PathPlanningParams => this.m_pathPlanningParams;

  public virtual CApAPPWaypointPlanSuccess GetPlanSuccess(int i_subtaskRank)
  {
    return CApAPPWaypointPlanSuccess.WaitingBeforeCalc;
  }

  public bool Start(
    ref CApAPPPlanCommandBase.PlanSuccessEventArgs errEvtArgs)
  {
    this.StoreSynchronizedTasksWaypoints();
    return this.DoStart(ref errEvtArgs);
  }

  public void StoreSynchronizedTasksWaypoints()
  {
    foreach (CApAPPTask capAppTask in this.m_tasksToPlan)
      capAppTask.UpdateSynchronizedWaypointList();
  }

  protected virtual bool DoStart(
    ref CApAPPPlanCommandBase.PlanSuccessEventArgs errEvtArgs)
  {
    return false;
  }

  protected void NotifyUpdateWaypointStatus()
  {
    if (this.WaypointDidChanged == null)
      return;
    this.WaypointDidChanged((object) this, new EventArgs());
  }

  protected void NotifyPlanningDidUpdate()
  {
    if (this.PlanningDidUpdate == null)
      return;
    this.PlanningDidUpdate((object) this, (EventArgs) new CApAPPPlanCommandBase.PlanDidUpdateEventArgs());
  }

  protected void UpdateProgress(object sender, EventArgs e)
  {
    if (this.PlanningDidProgress == null)
      return;
    CApAPPPlanCommandBase.PlanDidProgressEventArgs args = new CApAPPPlanCommandBase.PlanDidProgressEventArgs((double) this.GetElapsedTime() / 1000.0, this.GetProgress());
    this.PlanningDidProgress(sender, args);
  }

  protected void NotifyEndPlanning(object sender, CApAPPPlanCommandBase.PlanSuccessEventArgs e)
  {
    if (this.PlanningDidEnd == null)
      return;
    this.PlanningDidEnd(sender, e);
  }

  protected virtual bool IsPlanned(CApAPPSubTask subtask) => true;

  public virtual void UpdatePlanSuccesses()
  {
  }

  public virtual double GetProgress() => 0.0;

  public virtual double GetProgress(int i_subtaskRank) => 0.0;

  public virtual int RetrieveIndexSubProblemFromWaypoint(CApAPPWaypoint i_wp) => 0;

  public virtual int CountSubProblems() => 0;

  public virtual bool IsFinished(int i_subtaskRank) => false;

  public virtual long GetElapsedTime() => 0;

  public virtual void UpdateWaypointPlanSuccess(int i_subtaskRank)
  {
  }

  internal virtual void UpdateSubTaskToPlan()
  {
  }

  internal virtual bool IsMagnetPointEnabled() => false;

  internal virtual void EnableShowLastAddedPosition()
  {
  }

  internal virtual void DisableShowLastAddedPosition()
  {
  }

  internal virtual void ToggleMagnetPoint()
  {
  }

  internal virtual void ClearMagnetPoint()
  {
  }

  internal virtual void SetMagnetPointPositionToLastAddedNode()
  {
  }

  internal virtual KiAppPlanningStep GetCurrentPlanningStep() => (KiAppPlanningStep) 0;

  internal virtual void AfterPlanningFailure()
  {
  }

  public double ProgressRatio
  {
    get => this.m_progressRatio;
    set => this.m_progressRatio = value;
  }

  public bool PlanningWasCancelled { get; set; }

  public void UpdateWaypointPlanSuccess(CApAPPWaypoint i_wp)
  {
    foreach (CApAPPTask capAppTask in this.m_tasksToPlan)
    {
      int i_subtaskRank = capAppTask.RetrieveSubTaskIndexFromWaypoint(i_wp);
      if (i_subtaskRank != -1)
        i_wp.WaypointPlanSuccess = this.GetPlanSuccess(i_subtaskRank);
    }
  }

  public class PlanSuccessEventArgs : EventArgs
  {
    public PlanSuccessEventArgs(bool i_success, string i_message = "", string i_details = "")
    {
      this.Successful = i_success;
      this.ErrorMessage = i_message;
      this.ErrorDetails = i_details;
    }

    public bool Successful { get; set; }

    public string ErrorMessage { get; set; }

    public string ErrorDetails { get; set; }
  }

  public class PlanDidProgressEventArgs : EventArgs
  {
    public PlanDidProgressEventArgs(double timeInSecond, double progress)
    {
      this.TimeInSecond = timeInSecond;
      this.Progress = progress;
    }

    public double TimeInSecond { get; set; }

    public double Progress { get; set; }
  }

  public class PlanDidUpdateEventArgs : EventArgs
  {
  }
}
